package cn.com.css.taxInspection.statistics.controller;

import cn.com.css.taxInspection.attention.entity.AjcxRecord;
import cn.com.css.taxInspection.attention.entity.BacklogCase;
import cn.com.css.taxInspection.common.aop.annotation.LogAnnotation;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.gt3.entity.JcAjxxEntity;
import cn.com.css.taxInspection.gt3.service.JcAjxxService;
import cn.com.css.taxInspection.statistics.entity.Jcxlgjczrkqk;
import cn.com.css.taxInspection.statistics.service.JcxlgjczrkqkService;
import cn.com.css.taxInspection.statistics.service.StatisticsService;
import cn.com.css.taxInspection.statistics.vo.*;
import cn.com.css.taxInspection.system.entity.SysUser;
import cn.com.css.taxInspection.system.service.UserService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.entity.WorkflowCompensateEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowInspectionHearEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowLogEntity;
import cn.com.css.taxInspection.workflow.entity.WorkflowLogSurveyEntity;
import cn.com.css.taxInspection.workflow.service.WorkflowCompensateService;
import cn.com.css.taxInspection.workflow.service.WorkflowInspectionHearService;
import cn.com.css.taxInspection.workflow.service.WorkflowLogService;
import cn.com.css.taxInspection.workflow.service.WorkflowLogSurveyService;
import cn.com.css.taxInspection.workquality.vo.req.SelectchiefReqVo;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 数据统计
 *
 * @author wenbin
 * @version V1.0
 */
@RequestMapping("/statistics")
@RestController
@Api(tags = "首页工作质效统计")
public class StatisticsController {
    @Resource
    private StatisticsService statisticsService;
    @Resource
    private JcxlgjczrkqkService jcxlgjczrkqkService;
    @Resource
    private WorkflowInspectionHearService workflowInspectionHearService;
    @Resource
    private WorkflowCompensateService workflowCompensateService;
    @Resource
    private WorkflowLogService workflowLogService;
    @Resource
    private JcAjxxService jcAjxxService;
    @Resource
    private UserService userService;
    @Resource
    private WorkflowLogSurveyService workflowLogSurveyService;

    @PostMapping("/jcjfStatistics")
    @ApiOperation(value = "获取检查积分数据")
    @LogAnnotation(title = "质效管理", action = "获取检查积分数据")
    public DataResult jcjfStatistics(@RequestBody JcjfReq req) {
        String loadOrNOT = req.getLoadOrNOT();
        if ("0".equals(loadOrNOT)) {
            return DataResult.success();
        }
        return DataResult.success(statisticsService.jcjfStatistics(req));
    }

    @PostMapping("/jcjfStatisticsXz")
    @ApiOperation(value = "获取检查积分数据下钻")
    @LogAnnotation(title = "获取检查积分数据下钻", action = "获取检查积分数据下钻")
    public DataResult jcjfStatisticsXz(@RequestBody JcjfReq req) {
        String loadOrNOT = req.getLoadOrNOT();
        if ("0".equals(loadOrNOT)) {
            return DataResult.success();
        }
        return DataResult.success(statisticsService.jcjfStatisticsXz(req));
    }

    @PostMapping("/sljfStatistics")
    @ApiOperation(value = "获取审理积分数据")
    @LogAnnotation(title = "质效管理", action = "获取审理积分数据")
    public DataResult sljfStatistics(@RequestBody JcjfReq req) {
        String loadOrNOT = req.getLoadOrNOT();
        if ("0".equals(loadOrNOT)) {
            return DataResult.success();
        }
        return DataResult.success(statisticsService.sljfStatistics(req));
    }

    @PostMapping("/sljfStatisticsXz")
    @ApiOperation(value = "获取审理积分数据下钻")
    @LogAnnotation(title = "获取审理积分数据下钻", action = "获取审理积分数据下钻")
    public DataResult sljfStatisticsXz(@RequestBody JcjfReq req) {
        String loadOrNOT = req.getLoadOrNOT();
        if ("0".equals(loadOrNOT)) {
            return DataResult.success();
        }
        return DataResult.success(statisticsService.sljfStatisticsXz(req));
    }

    @PostMapping("/zxjfStatistics")
    @ApiOperation(value = "获取执行积分数据")
    @LogAnnotation(title = "质效管理", action = "获取执行积分数据")
    public DataResult zxjfStatistics(@RequestBody JcjfReq req) {
        String loadOrNOT = req.getLoadOrNOT();
        if ("0".equals(loadOrNOT)) {
            return DataResult.success();
        }
        return DataResult.success(statisticsService.zxjfStatistics(req));
    }

    @PostMapping("/zxjfStatisticsXz")
    @ApiOperation(value = "获取执行积分数据下钻")
    @LogAnnotation(title = "获取执行积分数据下钻", action = "获取执行积分数据下钻")
    public DataResult zxjfStatisticsXz(@RequestBody JcjfReq req) {
        String loadOrNOT = req.getLoadOrNOT();
        if ("0".equals(loadOrNOT)) {
            return DataResult.success();
        }
        return DataResult.success(statisticsService.zxjfStatisticsXz(req));
    }

    @PostMapping("/rukuStatistics")
    @ApiOperation(value = "入库统计")
    @LogAnnotation(title = "入库统计", action = "入库统计")
    public DataResult rukuStatistics(@RequestBody JcjfReq req) {
        return DataResult.success(statisticsService.rukuStatistics(req));
    }


    @PostMapping("/chabuStatistics")
    @ApiOperation(value = "查补统计")
    @LogAnnotation(title = "查补统计", action = "查补统计")
    public DataResult chabuStatistics(@RequestBody JcjfReq req) {
        return DataResult.success(statisticsService.chabuStatistics(req));
    }


    @PostMapping("/indexQsjcsr")
    @ApiOperation(value = "首页柱状图全市稽查收入")
    @LogAnnotation(title = "首页柱状图全市稽查收入", action = "首页柱状图全市稽查收入")
    public DataResult indexQsjcsr(@RequestBody IndexDataScopeReq req) {
        return DataResult.success(statisticsService.indexQsjcsr(req.getDataScope()));
    }

    @PostMapping("/jianjinzhan")
    @ApiOperation(value = "积案进展")
    @LogAnnotation(title = "积案进展", action = "积案进展")
    public DataResult jianjinzhan(@RequestBody JcjfReq req) {
        return DataResult.success(statisticsService.jianjinzhan(req.getSwjgDm()));
    }

    @PostMapping("/jianjinzhan_ddtxt")
    @ApiOperation(value = "积案进展-右侧一类积案堆叠条形图")
    @LogAnnotation(title = "积案进展右侧一类积案堆叠条形图", action = "积案进展右侧一类积案堆叠条形图")
    public DataResult jianjinzhan_ddtxt() {
        List<String> yljaajbhs = statisticsService.jianjinzhan_ddtxtgetlocalajbhs();
        return DataResult.success(statisticsService.jianjinzhan_ddtxtDatas(yljaajbhs));
    }

    @PostMapping("/anyuanAndanjian")
    @ApiOperation(value = "案件页面-案源和案件数据")
    @LogAnnotation(title = "案件页面-案源和案件数据", action = "案件页面-案源和案件数据")
    public DataResult anyuanAndanjian(@RequestBody JcjfReq req) {
        return DataResult.success(statisticsService.anyuanAndanjian(req.getSwjgDm()));
    }

    //积案进展查询
    @PostMapping("/jianqinglimingxi")
    @ApiOperation(value = "案件页面-积案进展查询")
    @LogAnnotation(title = "案件页面-积案进展查询", action = "案件页面-积案进展查询")
    public DataResult queryJajz(@RequestBody JajzReq jajzReq) {
        return DataResult.success(statisticsService.queryJajz(jajzReq.getSsdw()));
    }

    //工作质效查询
    @PostMapping("/queryJcxlgjczrkqk")
    @ApiOperation(value = "案件页面-工作质效查询")
    @LogAnnotation(title = "案件页面-工作质效查询", action = "案件页面-工作质效查询")
    public DataResult queryJcxlgjczrkqk(@RequestBody Jcxlgjczrkqk jcxlgjczrkqk) {
        return DataResult.success(statisticsService.queryJcxlgjczrkqk(jcxlgjczrkqk));
    }

    @PostMapping("/chabuandruku")
    @ApiOperation(value = "查补入库页面-查补入库数据")
    @LogAnnotation(title = "查补入库页面-查补入库数据", action = "查补入库页面-查补入库数据")
    public DataResult chabuandruku(@RequestBody JcjfReq req) {
        return DataResult.success(statisticsService.chabuandruku(req.getSwjgDm()));
    }

    @PostMapping("/anyuanAndanjian_swry")
    @ApiOperation(value = "案件页面-案源和案件数据三级人员")
    @LogAnnotation(title = "案件页面-案源和案件数据三级人员", action = "案件页面-案源和案件数据三级人员")
    public DataResult anyuanAndanjian_swry(@RequestBody JcjfReq req) {
        return DataResult.success(statisticsService.anyuanAndanjian_swry(req.getSwjgDm()));
    }

    @PostMapping("/indexCbAndRk")
    @ApiOperation(value = "首页折线图查补入库")
    @LogAnnotation(title = "首页折线图查补入库", action = "首页折线图查补入库")
    public DataResult indexCbAndRk(@RequestBody IndexDataScopeReq req) {
        return DataResult.success(statisticsService.indexCbAndRk(req.getDataScope()));
    }


    @PostMapping("/indexCbAndRkNew")
    @ApiOperation(value = "查补入库页面折线图")
    @LogAnnotation(title = "查补入库页面折线图", action = "查补入库页面折线图")
    public DataResult indexCbAndRkNew() {
        return DataResult.success(statisticsService.indexCbAndRkNew());
    }


    @PostMapping("/indexAjxxZzt")
    @ApiOperation(value = "首页案件信息柱状图")
    @LogAnnotation(title = "首页案件信息柱状图", action = "首页案件信息柱状图")
    public DataResult indexAjxxZzt(@RequestBody IndexDataScopeReq req) {
        return DataResult.success(statisticsService.indexAjxxZzt(req.getDataScope()));
    }

    @PostMapping("/indexAjxxZztNew")
    @ApiOperation(value = "首页案件信息柱状图(新)")
    @LogAnnotation(title = "首页案件信息柱状图(新)", action = "首页案件信息柱状图(新)")
    public DataResult indexAjxxZztNew() {
        return DataResult.success(statisticsService.indexAjxxZztNew());
    }

    @PostMapping("/indexJiAn")
    @ApiOperation(value = "首页积案圆柱状图")
    @LogAnnotation(title = "首页积案圆柱状图", action = "首页积案圆柱状图")
    public DataResult indexJiAn() {
        return DataResult.success(statisticsService.indexJiAn());
    }


    @PostMapping("/indexNknsh")
    @ApiOperation(value = "首页内控内生化")
    @LogAnnotation(title = "首页内控内生化", action = "首页内控内生化")
    public DataResult indexNknsh() {
        return DataResult.success(statisticsService.indexNknsh());
    }

    @PostMapping("/anjianxiazhuan")
    @ApiOperation(value = "首页案件下钻")
    @LogAnnotation(title = "首页案件下钻", action = "首页案件下钻")
    public DataResult anjianxiazhuan(@RequestBody AjxzReq req) {
        Page resPage = statisticsService.anjianxiazhuan(req);
        List<AjcxRecord> records = resPage.getRecords();
        for (AjcxRecord c : records) {
            String jcajxxuuid = c.getJcajxxuuid();
            WorkflowInspectionHearEntity workflowInspectionHearEntity = workflowInspectionHearService.getByJcajxxuuid(jcajxxuuid);
            c.setZsztmc(TsBqUtils.isNull(workflowInspectionHearEntity) ? c.getAjztmc() : workflowInspectionHearEntity.getStateName());
            c.setJcswjg(c.getJcswjg().replace("沈阳市税务局第一稽查局", ""));
        }
        resPage.setRecords(records);
        return DataResult.success(resPage);
    }


    @PostMapping("/gzzx/firstLevel")
    @ResponseBody
    public DataResult gzzxFirstLevel(@RequestBody SelectchiefReqVo req) {
        return DataResult.success(statisticsService.gzzxFirstLevel(req.getSwjgDm()));
    }

    @PostMapping("/gzzx/secondLevel")
    @ResponseBody
    public DataResult gzzxSecondLevel(@RequestBody SelectchiefReqVo req) {
        return DataResult.success(statisticsService.gzzxSecondLevel(req));
    }

    @PostMapping("/gzzx/thirdLevel")
    @ResponseBody
    public DataResult gzzxThirdLevel(@RequestBody SelectchiefReqVo req) {
        Page page = new Page();
        Map<String, Object> resMap = statisticsService.gzzxThirdLevel(req);
        List<Map<String, Object>> resTableDatas = (List<Map<String, Object>>) resMap.get("tableDatas");
        handlerDataByType(resTableDatas, req.getType());
        page.setRecords(resTableDatas);
        page.setTotal(Long.valueOf(String.valueOf(resMap.get("tableDatasTotal"))));
        page.setCurrent(req.getPage());
        return DataResult.success(page);
    }

    private void handlerDataByType(List<Map<String, Object>> resTableDatas, String type) {
        Iterator<Map<String, Object>> iterator = resTableDatas.iterator(); // 获取迭代器
        while (iterator.hasNext()) {
            Map<String, Object> currentMap = iterator.next(); // 获取当前Map对象
            String jcajxxuuid = String.valueOf(currentMap.get("JCAJXXUUID"));
            WorkflowCompensateEntity wcn = workflowCompensateService.getByJcajxxuuid(jcajxxuuid);
            if (TsBqUtils.isNotNull(wcn)) {
                switch (type) {
                    case "JCCBJF":
                    case "JCRKJF":
                        currentMap.put("YJWJSK", wcn.getCgzzqyjwjskje());
                        currentMap.put("QYKSSJTZ", wcn.getYcsdyqyksjsjtz());
                        break;
                    case "AJCCSXJF":
                        break;
                    case "AJDXJF":
                        currentMap.put("SSJDSSJ", wcn.getSsjdssj());
                        currentMap.put("AJDX", this.getAjdxMcByAjdxDm(wcn.getAjdx()));
                        break;
                }
            }
        }
    }

    private String getAjdxMcByAjdxDm(String ajdx) {
        String resAjdxStr = "";
        switch (ajdx) {
            case "1":
                resAjdxStr = "虚开";
                break;
            case "2":
                resAjdxStr = "团伙虚开骗税";
                break;
            case "3":
                resAjdxStr = "偷税";
                break;
            case "4":
                resAjdxStr = "其他问题";
                break;
            case "5":
                resAjdxStr = "无问题案件";
                break;
        }
        return resAjdxStr;
    }


    @ApiOperation(value = "日志记录-公用日志记录")
    @LogAnnotation(title = "日志记录", action = "日志记录")
    @PostMapping("/loglist")
    @ResponseBody
    @DS("oracle")
    public DataResult loglist(@RequestBody WorkflowLogEntity workflowLog) {
        Page<WorkflowLogEntity> page = new Page<>(workflowLog.getPage(), workflowLog.getLimit());
        LambdaQueryWrapper<WorkflowLogEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WorkflowLogEntity::getRelevance, workflowLog.getRelevance());
        IPage<WorkflowLogEntity> iPage = workflowLogService.page(page, queryWrapper);
        List<WorkflowLogEntity> records = iPage.getRecords();
        for (WorkflowLogEntity workflowLogEntity : records) {
            String relevance = workflowLogEntity.getRelevance();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            if (TsBqUtils.isNotNull(jcAjxxEntity)) {
                String nsrmc = jcAjxxEntity.getNsrmc();
                String nsrsbh = jcAjxxEntity.getNsrsbh();
                workflowLogEntity.setNsrmc(nsrmc);
                workflowLogEntity.setNsrsbh(nsrsbh);
            }
            String createId = workflowLogEntity.getCreateId();
            SysUser sysUser = userService.getUser(createId);
            String realName = sysUser.getRealName();
            if (TsBqUtils.isNotNull(realName)) {
                workflowLogEntity.setCreateId(realName);
            }
            // 处理完成工作
            // 处理案件日志，重新set CONTENT1
            String mold = workflowLogEntity.getMold();
            if (TsBqUtils.isNotNull(mold)) {
                WorkflowLogSurveyEntity logSurvey = workflowLogSurveyService.getById(mold);
                if (TsBqUtils.isNotNull(logSurvey)) {
                    mold = logSurvey.getTitle();
                }
                workflowLogEntity.setContent1(mold);
            }
        }
        return DataResult.success(iPage);
    }

    @PostMapping("/jbxcj")
    @ApiOperation(value = "稽比析成绩")
    @LogAnnotation(title = "稽比析成绩", action = "稽比析成绩")
    public DataResult jbxcj() {
        List<Map<String, Object>> resData = handlerResData();
        return DataResult.success(resData);
    }

    private List<Map<String, Object>> handlerResData() {
        List<Map<String, Object>> resData = new ArrayList<>();
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> dataMap1 = new HashMap<>();
        Map<String, Object> dataMap2 = new HashMap<>();
        Map<String, Object> dataMap3 = new HashMap<>();
        dataMap.put("swjgmc", "稽查局");
        dataMap.put("cbhs", "12");
        dataMap.put("rjhs", "25");
        dataMap.put("rjcb", "65");
        dataMap.put("hjcb", "34");
        dataMap.put("jaql", "12");
        dataMap.put("jaqk", "55");
        dataMap.put("rkqk", "12");
        dataMap.put("ccsc", "123");
        dataMap.put("eyhs", "78");
        dataMap.put("eysr", "12");
        dataMap.put("qt", "0");
        dataMap.put("hj", "428");
        dataMap1.put("swjgmc", "第一稽查局");
        dataMap1.put("cbhs", "12");
        dataMap1.put("rjhs", "25");
        dataMap1.put("rjcb", "65");
        dataMap1.put("hjcb", "34");
        dataMap1.put("jaql", "12");
        dataMap1.put("jaqk", "55");
        dataMap1.put("rkqk", "12");
        dataMap1.put("ccsc", "123");
        dataMap1.put("eyhs", "78");
        dataMap1.put("eysr", "12");
        dataMap1.put("qt", "0");
        dataMap1.put("hj", "428");
        dataMap2.put("swjgmc", "第二稽查局");
        dataMap2.put("cbhs", "12");
        dataMap2.put("rjhs", "25");
        dataMap2.put("rjcb", "65");
        dataMap2.put("hjcb", "34");
        dataMap2.put("jaql", "12");
        dataMap2.put("jaqk", "55");
        dataMap2.put("rkqk", "12");
        dataMap2.put("ccsc", "123");
        dataMap2.put("eyhs", "78");
        dataMap2.put("eysr", "12");
        dataMap2.put("qt", "0");
        dataMap2.put("hj", "428");
        dataMap3.put("swjgmc", "第三稽查局");
        dataMap3.put("cbhs", "12");
        dataMap3.put("rjhs", "25");
        dataMap3.put("rjcb", "65");
        dataMap3.put("hjcb", "34");
        dataMap3.put("jaql", "12");
        dataMap3.put("jaqk", "55");
        dataMap3.put("rkqk", "12");
        dataMap3.put("ccsc", "123");
        dataMap3.put("eyhs", "78");
        dataMap3.put("eysr", "12");
        dataMap3.put("qt", "0");
        dataMap3.put("hj", "428");
        resData.add(dataMap);
        resData.add(dataMap1);
        resData.add(dataMap2);
        resData.add(dataMap3);
        return resData;
    }


    @PostMapping("/jajzayjzb")
    @ApiOperation(value = "积案进展-按月进展表")
    @LogAnnotation(title = "积案进展-按月进展表", action = "积案进展-按月进展表")
    public DataResult jajzayjzb() {
        return DataResult.success(statisticsService.jajzayjzb());
    }

    @PostMapping("/queryJbx")
    @ApiOperation(value = "首页-查询稽比析")
    @LogAnnotation(title = "首页-查询稽比析", action = "首页-查询稽比析")
    public DataResult queryJbx() {
        return DataResult.success(statisticsService.queryJbx());
    }
}